<template>
  <Dialog v-model="dialogVisible" :title="t('pickDetail.voteDetail.title')" :width="800">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item :label="t('pickDetail.voteDetail.fields.voteResult')" prop="voteStatus">
        <el-select
          v-model="queryParams.voteStatus"
          clearable
          :placeholder="t('pickDetail.voteDetail.fields.voteResult')"
          style="width: 120px"
        >
          <el-option
            v-for="status in voteStatusOptions"
            :label="status.label"
            :value="status.value"
            :key="status.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button :disabled="loading" @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />{{ t('common.search') }}
        </el-button>
        <el-button :disabled="loading" @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />{{ t('common.reset') }}
        </el-button>
      </el-form-item>
    </el-form>

    <vxe-table
      id="iPickBatch-table"
      ref="tableRef"
      v-loading="loading"
      :data="list"
      stripe
      :custom-config="vxeTableConfig"
      :column-config="{ resizable: true }"
      :size="vxeTableSize"
      :max-height="440"
    >
      <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
      <vxe-column
        :title="t('pickDetail.voteDetail.fields.nickName')"
        field="nickName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="50"
      />
      <vxe-column
        :title="t('pickDetail.voteDetail.fields.phone')"
        field="phone"
        align="center"
        show-header-overflow
        show-overflow
        min-width="120"
      />
      <vxe-column
        :title="t('pickDetail.voteDetail.fields.deptName')"
        field="deptName"
        align="center"
        show-header-overflow
        show-overflow
        min-width="120"
      />
      <vxe-column
        :title="t('pickDetail.voteDetail.fields.jobTitle')"
        field="jobTitle"
        align="center"
        show-header-overflow
        show-overflow
        min-width="80"
      />
      <vxe-column
        :title="t('pickDetail.voteDetail.fields.workingStatus')"
        field="workingStatus"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      >
        <template #default="scope">
          <dict-tag
            v-if="scope.row.workStatus"
            :type="DICT_TYPE.WORKING_STATUS"
            :value="scope.row.workStatus"
          />
        </template>
      </vxe-column>
      <vxe-column
        :title="t('pickDetail.voteDetail.fields.voteResult')"
        field="voteStatus"
        align="center"
        show-header-overflow
        show-overflow
        min-width="60"
      >
        <template #default="{ row }">
          <el-tag disable-transitions v-if="row.voteResult === 0" type="warning">
            {{ t('pickDetail.voteDetail.voteStatus.unVote') }}
          </el-tag>
          <el-tag disable-transitions v-if="row.voteResult === 1" type="success">
            {{ t('pickDetail.voteDetail.voteStatus.agree') }}
          </el-tag>
          <el-tag disable-transitions v-if="row.voteResult === 2" type="danger">
            {{ t('pickDetail.voteDetail.voteStatus.disagree') }}
          </el-tag>
        </template>
      </vxe-column>
    </vxe-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </Dialog>
</template>

<script setup lang="ts">
import {PickDetailApi, PickVoteDetailVO} from '@/api/recruit/pickdetail'
import type { VxeTablePropTypes } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import { DICT_TYPE } from '@/utils/dict'

defineOptions({ name: 'PickVoteDetail' })

const props = defineProps<{
  batchId: string
}>()

const { t } = useI18n() // 国际化

const dialogVisible = ref(false)
const loading = ref(true) // 列表的加载中
const list = ref<PickVoteDetailVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive<any>({
  pageNo: 1,
  pageSize: 10,
  detailId: undefined,
  voteStatus: undefined
})
const queryFormRef = ref() // 搜索的表单

// 投票状态选项
const voteStatusOptions = [
  { label: t('pickDetail.voteDetail.voteStatus.unVote'), value: 0 },
  { label: t('pickDetail.voteDetail.voteStatus.agree'), value: 1 },
  { label: t('pickDetail.voteDetail.voteStatus.disagree'), value: 2 }
]

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PickDetailApi.getVoteDetailList({ ...queryParams, batchId: props.batchId })
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

// 打开弹窗
const open = (id: string) => {
  list.value = []
  queryParams.detailId = id
  resetQuery()
  dialogVisible.value = true
}

defineExpose({
  open
})
</script>

<style scoped lang="scss"></style>
